package cc.fs.myproject.utils;

import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import cc.fs.myproject.widget.BadgeView;

/**
 * Created by fostion on 2015/6/16.
 */
public class BottomTabsHelper {

    private View tabs[];
    public BadgeView badgeView[];
    private int mPosition;
    private OnTagChangeLinstener mOnTagChangeLinstener;

    public static BottomTabsHelper newInstance(ViewGroup vg,OnTagChangeLinstener onTagChangeLinstener,int selectPos){
        int count = vg.getChildCount();
        View tags[] = new View[count];
        for(int i=0;i<count;i++){
            tags[i] = vg.getChildAt(i);
        }
        BottomTabsHelper bottomTabsHelper = new BottomTabsHelper();
        bottomTabsHelper.mOnTagChangeLinstener = onTagChangeLinstener;
        bottomTabsHelper.initTabs(tags,selectPos);

        return bottomTabsHelper;
    }

    public void initTabs(View tabs[], final int selectTabs){

        this.tabs = tabs;
        this.mPosition = selectTabs;
        this.badgeView = new BadgeView[tabs.length];

        for(int i=0;i<tabs.length;i++){
            RelativeLayout rl = (RelativeLayout)tabs[i];
            rl.setTag(i);
            rl.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    switch(event.getAction()) {
                        case MotionEvent.ACTION_UP:
                            int position = Integer.valueOf(v.getTag().toString());
                            selectTab(position);
                            break;
                    }
                    return true;
                }
            });

            for(int j=0;j<rl.getChildCount();j++){
                if(rl.getChildAt(j) instanceof LinearLayout){
                    LinearLayout ly = (LinearLayout)rl.getChildAt(j);
                    if(i == mPosition){
                        rl.setSelected(true);
                        for(int k=0;k<ly.getChildCount();k++)
                            ly.getChildAt(k).setSelected(true);
                    }else{
                        rl.setSelected(false);
                        for(int k=0;k<ly.getChildCount();k++)
                            ly.getChildAt(k).setSelected(false);
                    }
                }else if(rl.getChildAt(i) instanceof BadgeView){
                    badgeView[i] = (BadgeView)rl.getChildAt(j);
                }
            }
        }
    }

    /**
     * the mPosition is last and position is now
     */

    void selectTab(int position){

        if(mPosition != position){

            RelativeLayout unSelectRoot = (RelativeLayout)tabs[mPosition];
            for(int i=0;i<unSelectRoot.getChildCount();i++){
                if(unSelectRoot.getChildAt(i) instanceof LinearLayout){
                    LinearLayout unSelectLayout = (LinearLayout)unSelectRoot.getChildAt(i);
                    unSelectRoot.setSelected(false);
                    for(int j=0;j<unSelectLayout.getChildCount();j++){
                        unSelectLayout.getChildAt(j).setSelected(false);
                    }
                }
            }

            RelativeLayout SelectRoot = (RelativeLayout)tabs[position];
            for(int i=0;i<SelectRoot.getChildCount();i++){
                if(SelectRoot.getChildAt(i) instanceof LinearLayout){
                    LinearLayout selectLayout = (LinearLayout)SelectRoot.getChildAt(i);
                    SelectRoot.setSelected(true);
                    for(int j=0;j<selectLayout.getChildCount();j++){
                        selectLayout.getChildAt(j).setSelected(true);
                    }
                }
            }

            this.mPosition = position;
            mOnTagChangeLinstener.onTagChange(position);

        }
    }

    public interface OnTagChangeLinstener{
        void onTagChange(int position);
    }

}
